<?php

namespace App;
use Session;
use Illuminate\Database\Eloquent\Model;
use DB;
use Cache;
class Index_Home extends Model{
   

	//前台查询所有分类
	public function select(){
		 $data=DB::table('cate')->select('cate_id','cate_name','parent_id')->where('cate_status',0 )->get();
		 return $this->getChilds($data);
		 
	}

    //前台热销品牌
    public function hot_brand(){
       return DB::table('brand')->where('is_show',1)->orderBy('brand_sort','desc')->get(['brand_id','brand_name']);
    }

   //前台展示轮播图
	public function home_select(){
		$res = DB::table("advert")->select("adv_id","name","image","url")->where("is_show","1")->where("type","0")->get();
		return $res;
	}



	//递归 父子结构
	function getChilds($data,$pid=0){
		$arr=array();
		foreach($data as $key=>$val){
			if($val->parent_id==$pid){
				$child=$this->getChilds($data,$val->cate_id);
				if($child){
					$val->child=$child;
				}
				$arr[]=$val;
			}
		}
		return $arr;
	}

	//前台友情链接展示
	public function friend_links(){
		$data=DB::table('friend_link')->select('link_id','link_name','link_url')->where('status',0 )->get();
		return $data;
	}


	//前台广告位1楼
	public function adv1(){
		$res = DB::table("advert")->where("level","1")->where("is_show","1")->first();
		return $res;
	}
	//前台广告位2楼
	public function adv2(){
		$res = DB::table("advert")->where("level","2")->where("is_show","1")->first();
		return $res;
	}
	//前台广告位3楼
	public function adv3(){
		$res = DB::table("advert")->where("level","3")->where("is_show","1")->first();
		return $res;
	}

	//搜索框下的关键词
	public function hot_search_terms(){
		$res = DB::table("keywords")->limit(6)->orderBy("count","desc")->get();
		return $res;
	}
	//热门商品
	public function hot_goods(){
		$res = DB::table("goods")->where("is_hot","1")->limit(15)->orderBy("sort","desc")->get();
		return $res;
	}

	//热门商品
	public function like_goods(){
		$res = DB::table("goods")->limit(15)->orderBy("sort","asc")->get();
		return $res;
	}

	//前台楼层1楼 智能手机 ID：3
	public function floor1(){	
		return DB::table('goods')->where('cate_id',3)->where('is_on_sale','1')->orderBy('sort','desc')->get(['goods_id','goods_name','goods_price','goods_img']);
	}

	//前台楼层2楼 女装 ID：28
	public function floor2(){	
		return DB::table('goods')->where('cate_id',28)->where('is_on_sale','1')->orderBy('sort','desc')->get(['goods_id','goods_name','goods_price','goods_img']);
	}

	//前台楼层3楼 食品 ID：8
	public function floor3(){	
		return DB::table('goods')->where('cate_id',8)->where('is_on_sale','1')->orderBy('sort','desc')->get(['goods_id','goods_name','goods_price','goods_img']);
	}

}
